<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="auto"
    >
      <el-row>
        <el-row>
          <el-form-item label="月份" prop="date">
            <el-date-picker
              style="width: 180px"
              clearable
              v-model="queryParams.date"
              value-format="YYYY-MM"
              type="month"
              placeholder="请选择时间"
            />
          </el-form-item>
        </el-row>
        <div class="operation">
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </div>
      </el-row>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Download" @click="handleExport"
          >导出</el-button
        >
      </el-col>
    </el-row>
    <div>
      <!-- show-summary
      :summary-method="getSummaries" -->
      <el-table v-loading="loading" border height="60vh" :data="dataList">
        <el-table-column label="序号" width="60" type="index" align="center" />
        <el-table-column label="车间" align="center" prop="workshopName">
        </el-table-column>
        <el-table-column label="机台" align="center" prop="machineName">
        </el-table-column>

        <el-table-column label="机台" align="center" prop="workshopName">
        </el-table-column>
        <el-table-column label="印刷方式" align="center" prop="printType">
        </el-table-column>
        <el-table-column label="版材尺寸" align="center" prop="size">
        </el-table-column>
        <el-table-column label="机长" align="center" prop="headName">
        </el-table-column>
        <el-table-column label="实际用纸令数" align="center" prop="realityLing">
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script setup name="RealityLing">
import { downLoad, toNumber } from "@/utils/ruoyi";
import {
  getRealityLing,
  exportRealityLing,
} from "@/api/statement/realityLing.js";
const { proxy } = getCurrentInstance();
const loading = ref(true);
const dataList = ref([]);
const total = ref(0);

const getSummaries = (param) => {
  const { columns, data } = param;
  const sums = [];
  const fieldsToSum = [
    "status",
    "machineName",
    "printType",
    "size",
    "headName",
  ];

  columns.forEach((column, index) => {
    if (index === 0) {
      sums[index] = "合计";
      return;
    }
    if (!fieldsToSum.includes(column.property)) {
      const values = data.map((item) => Number(item[column.property]));
      if (!values.every((value) => isNaN(value))) {
        sums[index] = values.reduce((prev, curr) => {
          const value = Number(curr);
          if (!isNaN(value)) {
            return toNumber(prev + curr, 4);
          } else {
            return toNumber(prev, 4);
          }
        }, 0);
        sums[index];
      } else {
        sums[index] = "";
      }
    }
  });

  return sums;
};

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 50,
  },
});
const { queryParams } = toRefs(data);

const getCurrentDay = () => {
  var now = new Date();
  var year = now.getFullYear();
  var month = now.getMonth() + 1; // 月份从0开始，需要加1
  var day = now.getDate();
  if (month < 10) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  // queryParams.value.deliveryDate = year + "-" + month + "-" + day;
  queryParams.value.date = year + "-" + month;
};
const hashMap = ref({});
getCurrentDay();

const getList = () => {
  loading.value = true;
  getRealityLing(queryParams.value)
    .then((res) => {
      loading.value = false;
      dataList.value = res.rows;
      if (res.rows.length > 0) {
        hashMap.value = res.rows[0].hashMap;
      }
      total.value = res.total;
    })
    .catch((err) => {
      loading.value = false;
    });
};

// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
};

// 导出
const handleExport = () => {
  exportRealityLing(queryParams.value).then((res) => {
    downLoad(res.msg, "机台用纸令数列表");
  });
};

getList();
// router.beforeEach((to, from, next) => {
//     if (to.path == "/basicInfo/personnel") {
//         getList()
//     }
//     next();
// });
</script>

<style lang="scss" scoped>
.operation {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-bottom: 18px;
}
</style>
